package com.ruoyi.mobile.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.security.utils.DictUtils;
import com.ruoyi.mobile.domain.MaterialInfojkEntity;
import com.ruoyi.mobile.domain.ShCourseEntity;
import com.ruoyi.mobile.domain.ShCourseTeacherEntity;
import com.ruoyi.mobile.mapper.IShCourseMapper;
import com.ruoyi.mobile.mapper.IShCourseTeacherMapper;
import com.ruoyi.mobile.mapper.MaterialInfojkMapper;
import com.ruoyi.mobile.utils.NftResponse;
import com.ruoyi.mobile.utils.Paginator;
import com.ruoyi.mobile.utils.QueryUtil;
import com.ruoyi.mobile.utils.ResponseConstants;
import com.ruoyi.system.api.domain.SysDictData;
import com.ruoyi.system.api.req.ShCourseTeacherReq;
import com.ruoyi.system.api.resp.ShCourseTeacherResp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.service.IShCourseTeacherService;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 课程教师信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-18
 */
@Service
public class ShCourseTeacherServiceImpl extends ServiceImpl<IShCourseTeacherMapper, ShCourseTeacherEntity> implements IShCourseTeacherService {
    @Autowired
    private IShCourseMapper courseMapper;
    @Autowired
    private MaterialInfojkMapper materialInfojkMapper;

    @Override
    public NftResponse<Paginator<ShCourseTeacherResp>> getTeacherList(ShCourseTeacherReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        Paginator<ShCourseTeacherResp> paginator = new Paginator<ShCourseTeacherResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        QueryWrapper<ShCourseTeacherEntity> wrapperQuery = new QueryWrapper<>();
        StringBuffer condition = new StringBuffer();
        if (null!=query.getTeacherType()&&!"".equals(query.getTeacherType())) {
            String typeStr = query.getTeacherType();
            String[] typeAry = typeStr.split(",");
            condition.append("(");
            for (String typeId:typeAry) {
                condition.append("find_in_set('"+typeId+"',teacher_type) or ");
            }
            condition.append("1=0)");
            wrapperQuery.apply(condition.toString());
        }
        if (null!=query.getTeacherTag()&&!"".equals(query.getTeacherTag())) {
            String tagStr = query.getTeacherTag();
            String[] tagAry = tagStr.split(",");
            condition.append("(");
            for (String tagId:tagAry) {
                condition.append("find_in_set('"+tagId+"',teacher_tag) or ");
            }
            condition.append("1=0)");
            wrapperQuery.apply(condition.toString());
        }
        if (null!=query.getAppid()&&!"".equals(query.getAppid())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("appId"),query.getAppid());
        }
        wrapperQuery.orderByAsc("order_num");
        IPage<ShCourseTeacherEntity> page = new Page<>(query.getPageNo(),query.getPageSize());
        IPage<ShCourseTeacherEntity> pageRtn = this.baseMapper.selectPage(page,wrapperQuery);
        List<ShCourseTeacherEntity> list = pageRtn.getRecords();
        List<ShCourseTeacherResp> result = BeanUtil.copyToList(list, ShCourseTeacherResp.class);
        List<SysDictData> dictTList = DictUtils.getDictCache("teacher_tag");
        HashMap<String, String> dictTMap = new HashMap<>();
        for (SysDictData data:dictTList) {
            dictTMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (ShCourseTeacherResp obj:result) {
            QueryWrapper<ShCourseEntity> wrapperQueryC = new QueryWrapper<>();
            wrapperQueryC.eq(QueryUtil.camelToUnderline("teacherId"), obj.getId());
            Long count = courseMapper.selectCount(wrapperQueryC);
            obj.setCourseCount(count);
            String tagStr = obj.getTeacherTag();
            if (null!=tagStr&&!"".equals(tagStr)) {
                String[] tagAry = tagStr.split(",");
                StringBuffer tagBuffer = new StringBuffer();
                for (String tag:tagAry) {
                    tagBuffer.append(dictTMap.get(tag)).append(",");
                }
                obj.setTeacherTag(tagBuffer.toString());
            }
        }
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageRtn.getTotal());
        paginator.setPageData(result);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<Paginator<ShCourseTeacherResp>> getTeacherjkList(ShCourseTeacherReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        Paginator<ShCourseTeacherResp> paginator = new Paginator<ShCourseTeacherResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        QueryWrapper<ShCourseTeacherEntity> wrapperQuery = new QueryWrapper<>();
        StringBuffer condition = new StringBuffer();
        if (null!=query.getTeacherType()&&!"".equals(query.getTeacherType())) {
            String typeStr = query.getTeacherType();
            String[] typeAry = typeStr.split(",");
            condition.append("(");
            for (String typeId:typeAry) {
                condition.append("find_in_set('"+typeId+"',teacher_type) or ");
            }
            condition.append("1=0)");
            wrapperQuery.apply(condition.toString());
        }
        if (null!=query.getTeacherTag()&&!"".equals(query.getTeacherTag())) {
            String tagStr = query.getTeacherTag();
            String[] tagAry = tagStr.split(",");
            condition.append("(");
            for (String tagId:tagAry) {
                condition.append("find_in_set('"+tagId+"',teacher_tag) or ");
            }
            condition.append("1=0)");
            wrapperQuery.apply(condition.toString());
        }
        if (null!=query.getAppid()&&!"".equals(query.getAppid())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("appId"),query.getAppid());
        }
        if (null!=query.getUserId()&&!"".equals(query.getUserId())) {
            wrapperQuery.inSql("id", "select jxteacher_id from school_classesjk where id in (select classes_id from school_studentjk where student_id='"+query.getUserId()+"')");
        }else {
            wrapperQuery.inSql("id", "select jxteacher_id from school_classesjk");
        }
        wrapperQuery.orderByAsc("order_num");
        IPage<ShCourseTeacherEntity> page = new Page<>(query.getPageNo(),query.getPageSize());
        IPage<ShCourseTeacherEntity> pageRtn = this.baseMapper.selectPage(page,wrapperQuery);
        List<ShCourseTeacherEntity> list = pageRtn.getRecords();
        List<ShCourseTeacherResp> result = BeanUtil.copyToList(list, ShCourseTeacherResp.class);
        List<SysDictData> dictTList = DictUtils.getDictCache("teacher_tag");
        HashMap<String, String> dictTMap = new HashMap<>();
        for (SysDictData data:dictTList) {
            dictTMap.put(data.getDictValue(),data.getDictLabel());
        }
        for (ShCourseTeacherResp obj:result) {
            QueryWrapper<MaterialInfojkEntity> wrapperQueryJk = new QueryWrapper<>();
            wrapperQueryJk.eq(QueryUtil.camelToUnderline("version"), obj.getTeacherName());
            List<MaterialInfojkEntity> listJk = this.materialInfojkMapper.selectList(wrapperQueryJk);
            BigDecimal amount = BigDecimal.valueOf(0);
            if (null!=listJk&&listJk.size()>0) {
                for (MaterialInfojkEntity entityJk:listJk) {
                    if (null != entityJk.getAmount()) {
                        amount.add(entityJk.getAmount());
                    }
                }
                obj.setCourseCount(Long.valueOf(listJk.size()));
                obj.setAmount(amount);
            }
            String tagStr = obj.getTeacherTag();
            if (null!=tagStr&&!"".equals(tagStr)) {
                String[] tagAry = tagStr.split(",");
                StringBuffer tagBuffer = new StringBuffer();
                for (String tag:tagAry) {
                    tagBuffer.append(dictTMap.get(tag)).append(",");
                }
                obj.setTeacherTag(tagBuffer.toString());
            }
        }
        paginator.setPageNo(query.getPageNo());
        paginator.setPageSize(query.getPageSize());
        paginator.setTotalDataNum(pageRtn.getTotal());
        paginator.setPageData(result);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<ShCourseTeacherResp> getTeacherInfo(Long id) {
        ShCourseTeacherEntity enity = this.baseMapper.selectById(id);
        ShCourseTeacherResp shopResp = BeanUtil.copyProperties(enity, ShCourseTeacherResp.class);
        if (shopResp == null) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "查无此教师", null);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", shopResp);
    }

    @Override
    public NftResponse<ShCourseTeacherResp> getTeacherjkInfo(Long id) {
        ShCourseTeacherEntity enity = this.baseMapper.selectById(id);
        ShCourseTeacherResp rtn = BeanUtil.copyProperties(enity, ShCourseTeacherResp.class);
        QueryWrapper<MaterialInfojkEntity> wrapperQuery = new QueryWrapper<>();
        if (null!=rtn.getTeacherName()&&!"".equals(rtn.getTeacherName())) {
            wrapperQuery.eq(QueryUtil.camelToUnderline("version"), rtn.getTeacherName());
        }
        List<MaterialInfojkEntity> listJk = this.materialInfojkMapper.selectList(wrapperQuery);
        BigDecimal amount = BigDecimal.valueOf(0);
        if (null!=listJk&&listJk.size()>0) {
            for (MaterialInfojkEntity entityJk:listJk) {
                if (null != entityJk.getAmount()) {
                    amount.add(entityJk.getAmount());
                }
            }
            rtn.setJkNum(listJk.size());
            rtn.setAmount(amount);
        }
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", rtn);
    }
}